package com.tjb.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tjb.dao.MessageDao;
import com.tjb.dao.MessageDaoImpl;
import com.tjb.entity.MessageBeam;
import com.tjb.util.FastJsonUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

@WebServlet("/MessageServlet")
public class MessageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        request.setCharacterEncoding("utf-8");

        String ids[] = request.getParameterValues("selectId[]");
        MessageDao dao = new MessageDaoImpl();
        int[] aa = new int[ids.length];
        for(int i=0;i<ids.length;i++){
            int is = Integer.parseInt(ids[i]);
            aa[i] = is;
            System.out.println(aa[i]);
        }
        int ss = dao.delete(aa);
        System.out.println(ss);
        List<MessageBeam> list = dao.getMessages();
        response.getWriter().print(new FastJsonUtil().setFormat(list,"yyyy-MM-dd"));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        response.setCharacterEncoding("utf-8");

        MessageDao dao = new MessageDaoImpl();
        List<MessageBeam> list = dao.getMessages();

        ObjectMapper obj = new ObjectMapper().setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));

        response.getWriter().print(obj.writeValueAsString(list));
    }
}
